import requestUrl from '../../utils/requestUtil.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //用户信息
    userInfo:{},
    //动态内容的数组
    dynamicList:[],
    //请求的总页数
    PageCount:0,
    //每次请求的条数
    pages:10,
    //请求的当前页数
    currentPage:1,
    //是否弹出层
    show:false,
    //照片展示数组
    picList: [],
    //swiper是否有指示点
    indicatorDots: true,
    //swiper是否纵向滑动
    vertical: false,
    //swiper是否自动切换
    autoplay: false,
    //swiper自动切换间隔
    interval: 2000,
    //swiper滑动动画时长
    duration: 500,

  },
  //展示弹出层
  showImg(e){
    
    this.setData({ 
      picList:e.currentTarget.dataset.imglist,
      show: true 
    });
    // wx.previewImage({
    //   current: e.currentTarget.dataset.imglist[0], // 当前显示图片的http链接
    //   urls: e.currentTarget.dataset.imglist // 需要预览的图片http链接列表
    // })
  },
  // 关闭弹出层
  onClose() {
    this.setData({ show: false });
  },
  /**
   * 请求用户信息
   */
  requestUserInfo(){
    const userToken = wx.getStorageSync('token')
    var that = this;
    requestUrl.requestUrl({
        url: "/app/ph/info",
        params: {},
        method:"get",
        header:{
            token:userToken
        }
    }).then((data) => {
        if (data.data.code == 0) {
            that.setData({
                userInfo:data.data.data
            })
        } 
    }).catch((errorMsg) => {
      console.log(errorMsg);
    })
  },
  /**
   *  请求动态内容
   * @param {*} currentPage 请求页数
   */
  requestDynamic(currentPage){
    const userToken = wx.getStorageSync('token')
    var that = this;
    requestUrl.requestUrl({
        url: "/app/ph/diary/myList",
        params: {
          "asc": true,
          "limit": that.data.pages,
          "page": currentPage,
          "sidx": "instTime"
        },
        method:"post",
        header:{
          token:userToken
        }
    }).then((data) => {
       let list = data.data.list.records;
       let completeArray = that.addtoArray(list,that.duplicateRemovalList(that.formatDateList(list)));
       that.setData({
          currentPage:data.data.list.current,
          PageCount:data.data.list.pages,
          dynamicList:that.data.dynamicList.concat(completeArray)
       })
    }).catch((errorMsg) => {
      console.log("error",errorMsg);
    })
  },
  /**
   * 将数组按照格式转换成另一个数组
   * @param {*} list 
   */
  formatDateList:function(list){
      var bigArr = [];
      //根据时间变成相应对象，push进bigArr这个数组中去
      list.forEach(item => {
          let date = new Date(item.instTime.replace(/-/g,"/"));
          var year = date.getFullYear();
          var month = date.getMonth()+1;
          var day = date.getDate();
          let obj = {
              year:year,
              month:month,
              day:day,
              todaylist:[]
          }
          if (bigArr.length == 0) {
              bigArr.push(obj)
          }else{
              for (let index = 0; index < bigArr.length; index++) {
                const element = bigArr[index];
                if (element.year == year && element.month == month && element.day == day) {
                    continue;
                }else{
                    bigArr.push(obj);
                }
              }
          }
      });
      return bigArr;
  },
  /**
   * 
   * @param {*} list 需要时间去重的数组
   */
  duplicateRemovalList:function(list){
    //去除bigArr中时间重复的对象
      var i = 0
      var len = list.length;
      while (i <= len) {
          if (!((i+1)>=len)) {
            let before = list[i];
            let after = list[i+1];
            if (before.year == after.year && before.month == after.month && before.day == after.day) {
                list.splice(i+1,1);
                i = 0;
                len = list.length
            }else{
                i++;
            }
          }else{
            i++;
          }
      }
      return list;
  },
  /**
   * 
   * @param {*} list 需要插入的数组
   * @param {*} bigArr 去重后日期数组
   */
  addtoArray:function(list,bigArr){
    //根据时间，将每条记录push进对应的时间对象里
    list.forEach(item => {
        let date = new Date(item.instTime.replace(/-/g,"/"));
        let year = date.getFullYear();
        let month = date.getMonth()+1;
        let day = date.getDate();
        const obj = {
          describe:item.context,
          imgLit:JSON.parse(item.attachment),
        }
        bigArr.forEach(element => {
          if (element.year == year && element.month == month && element.day == day) {
              element.todaylist.push(obj);
          }
        });
    });
    return bigArr;
  },
  /**
   * 跳转添加页面
   */
  gotoAddfruitClub(){
    wx.navigateTo({
      url: '../fruitClub_increase/fruitClub_increase',
    })
  },
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestUserInfo();
    this.requestDynamic(1);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      dynamicList:[],
      currentPage:1,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.currentPage <= this.data.PageCount) {
      wx.showLoading({
        title: '玩命加载中..',
      })
      this.requestDynamic(this.data.currentPage+1);
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    }else{
      wx.showLoading({
        title: '没有更多内容了',
      })
      
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})